package com.wu.cloud.custom.configuration;

import bean.PeskResponseEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import util.ResponseUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xwu36
 */
@ControllerAdvice
@ResponseBody
public class ControllerAdviceHandler {

    static final Logger logger = LoggerFactory.getLogger(ControllerAdviceHandler.class);

    @ExceptionHandler(value = Exception.class)
    public PeskResponseEntity errorHandle(HttpServletRequest request, HttpServletResponse response, Exception e) {
        Map<String, Object> params = new HashMap<>();
        params.put("url", request.getRequestURI());
        params.put("msg", e.getMessage());
        logger.error(String.format("请求路径:%s,错误信息:%s\n",request.getRequestURI(),e.getMessage()), e);
        return ResponseUtil.fail("调用API发生错误");
    }
}
